package com.hpdz.hotel_back.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hpdz.hotel_back.entity.Customer;
import com.hpdz.hotel_back.entity.Order;
import com.hpdz.hotel_back.service.CustomerService;
import com.hpdz.hotel_back.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 *  用户控制器!
 */
@Slf4j
@RestController
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private  OrderService orderService;


    /**
     *  接口1: 用户注册
     *  name: /cust_register
     *
     *  method: post;
     *
     *  parameter: customer;
     *
     *  return true/false;
     */
    @PostMapping("/cust_register")
    public Boolean register(@RequestBody Customer customer){
        return customerService.save(customer);
    }


    /**
     *  接口 2: 用户登录
     *  name: /cust_login
     *
     *  method: post;
     *
     *  parameter: customer;
     *
     *  return customer or null;
     */
    @PostMapping("/cust_login")
    public Customer login(@RequestBody Customer customer, HttpServletRequest request){
        Customer login = customerService.login(customer);
        log.debug("login",login);
        System.out.println(login);
        if(login != null){
            HttpSession session = request.getSession(true);
            session.setAttribute("cur_cust",login);
            login.setPassword("");
            return login;
        }else {
            return null;
        }
    }




    /**
     *  接口 3: 用户信息修改
     *  name: /modify_cust
     *
     *  method: post;
     *
     *  parameter: customer
     *
     *  return true/false;
     *
     *  // 参考一下,admin的类似方法即可!
     */
    @PostMapping("/modify_cust")
    public Boolean modifyCustomer(@RequestBody Customer customer){
        return customerService.updateById(customer);
    }



    /**
     *  接口4: 查看所有订单,分页
     *  name: /look_list
     *
     *  method: get;
     *
     *  parameter: current,size;
     *
     *  return List;
     */
    @GetMapping("/look_list/{current}/{size}")
    public IPage<Order> showAllOrder(@PathVariable Integer current, @PathVariable Integer size, HttpSession session){
        return customerService.selectByPage(current,size,session);
    }

    /**
     *
     *  需要前端以order数组的形式发送过来!
     */
    /**
     *  接口 5: 订单信息存储
     *  name: /submit_order
     *
     *  method: post;
     *
     *  parameter: orders;
     *
     *  return true/false;
     */
    @PostMapping("/submit_order")
    public Boolean submitOrder(@RequestBody List<Order> orders,HttpSession session){
       return orderService.saveBatch(orders);
    }

    /**
     * 接口 6: 取消订单
     * name: /order_cancel
     *
     * method: get;
     *
     * parameter: orders;
     *
     * return true/false;
     */
    @GetMapping("/order_cancel")
    public Boolean cancelOrder(@RequestBody List<Integer> orders,HttpServletRequest request){
        return orderService.change(orders,"cancel");
    }

}
